//
//  LBNewFeatureCollection.swift
//  LBNewWeiBo
//
//  Created by bin on 15/10/30.
//  Copyright © 2015年 bin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell" // 重用标记

class LBNewFeatureCollection: UICollectionViewController {

    //MARK: - 属性
    private let itemCount = 4
    
    /// layout
    private var layout = UICollectionViewFlowLayout()
    
    //MARK: - 构造方法
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView?.registerClass(LBNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        prepareLayout() // 设置layout参数
    }

    //MARK: - 设置prepareLayout参数
    
   private func prepareLayout() {
        // 设置item的大小
    layout.itemSize = UIScreen.mainScreen().bounds.size
    
    // cell的间距
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    // 分页效果
    collectionView?.pagingEnabled = true
    
    // 取消弹簧效果
    collectionView?.bounces = false
    
    }
    
    //MARK: - 数据源方法
    /// 显示的页数
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    /// 内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        // 创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as! LBNewFeatureCell
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    // 显示完毕cell
    // 分页滚动完毕cell看不到的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获取正在显示cell的索引
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        // 获取cell
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath)as? LBNewFeatureCell
        // 最后一页显示动画
        if showIndexPath.item == itemCount - 1  {
            // 开始按钮的动画
            cell!.startButtonAnima()
        }
    }
    
}

// 自定义cell
class LBNewFeatureCell : UICollectionViewCell {
    
    //MARK: - 属性
    /// 图片的索引
    var imageIndex : Int = 0 {
        didSet {
            //  利用当前的索引设置图片
            bgImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
            
        }
    }
    
    //MARK: - 构造方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 准备UI
        
        prepareUI()
    }

    //MARK: - 开始按钮动画
    func startButtonAnima() {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
        }
    }

    
    //MARK: - 准备UI
    func prepareUI() {
        
        // 添加子控件
        contentView.addSubview(bgImageView) // 背景图片
        contentView.addSubview(startButton) // 按钮
        
        // 设置约束
        // 背景图片的约束
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView":bgImageView]))
        // 垂直
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView":bgImageView]))
        
        // 按钮的约束
        // 开始体验按钮
        startButton.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    /// 点击事件
    func startButtonClick() {
        // 跳转到主页控制器
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
    
    }
    
    //MARK: - 懒加载
    lazy var bgImageView = UIImageView()
    
    lazy var startButton: UIButton = {
        // 创建按钮
        let button = UIButton()
        // 设置背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        // 设置点击事件
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}

